interloc
This crate defines an interface for creating allocator middleware,
i.e. code that runs when your allocator is run.
Examples
use interloc::{AllocMonitor, AllocAction, InterAlloc, StatsMonitor, ThreadMonitor};
use std::alloc::System;
use core::alloc::Layout;
struct MyMonitor {
pub global: StatsMonitor,
pub local: ThreadMonitor
}
impl MyMonitor {
pub const fn new() -> Self {
Self {
global: StatsMonitor::new(),
local: ThreadMonitor::new(),
}
}
}
impl AllocMonitor for MyMonitor {
fn monitor(&self, layout: Layout, action: AllocAction) {
self.global.monitor(layout, action);
self.local.monitor(layout, action);
}
}
static MONITOR: MyMonitor = MyMonitor::new();
#[global_allocator]
static GLOBAL: InterAlloc<System, MyMonitor> = InterAlloc {
inner: System,
monitor: &MONITOR,
};